package com.lzp.customview.curve

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.LinearGradient
import android.graphics.Paint
import android.graphics.Path
import android.graphics.Shader.*
import android.text.TextPaint
import android.util.AttributeSet
import android.util.Log
import android.view.View
import com.utils.SizeUtils

/**
 * @describe:曲线图
 * @Author: lixiaopeng
 * @Date: 2020/10/26
 */
class CurveItemView : View {

    private val TAG = "CurveItemView"
    private var startX = 0f
    private var dx = 10f
    private var endX = 100f

    private var startY = 0f
    private var endY = 100f

    private var itemWidth = 0f//默认一View宽度显示10个点
    private val dp1: Int
    private val textHeight: Int

    private val lineColor = 0x883d445a.toInt()
    private val pathColor = 0xff00d2f7.toInt()
    private val textColor = 0xffffffff.toInt()

    private val fillStartColor = 0xcc18beff.toInt()
    private val fillEndColor = 0x0000e3d7.toInt()

    private val linePaint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.DITHER_FLAG)
    private val pathPaint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.DITHER_FLAG)
    private val fillPaint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.DITHER_FLAG)
    private val textPaint = TextPaint(Paint.ANTI_ALIAS_FLAG or Paint.DITHER_FLAG)

    private var viewWidth = 0
    private var viewHeight = 0

    private val curvePath = Path()
    private val fillPath = Path()

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        dp1 = SizeUtils.dip2px(context, 1)
        textHeight = dp1 * 18
        initPaint()
    }

    private fun initPaint() {
        pathPaint.color = pathColor
        pathPaint.style = Paint.Style.STROKE
        pathPaint.strokeWidth = dp1*3f
        pathPaint.strokeCap = Paint.Cap.ROUND
        pathPaint.strokeJoin = Paint.Join.ROUND
        pathPaint.setShadowLayer(10f,0f,3f, Color.RED)

        linePaint.color = lineColor
        linePaint.style = Paint.Style.STROKE
        linePaint.strokeWidth = dp1*1f
        linePaint.strokeCap = Paint.Cap.ROUND
        linePaint.strokeJoin = Paint.Join.ROUND

        textPaint.color = textColor
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        if (w != 0) {
            viewWidth = w
        }
        if (h != 0) {
            viewHeight = h
        }
        if (itemWidth == 0f) {
            itemWidth = viewWidth / 10f
        }
        val linearGradient = LinearGradient(0f, 0f, 0f, viewHeight.toFloat(), fillStartColor, fillEndColor, TileMode.CLAMP)
        fillPaint.shader = linearGradient
    }

    fun setStartAndEndX(startX: Float, endX: Float) {
        this.startX = startX
        this.endX = endX
    }

    fun setDx(dx: Float) {
        this.dx = dx
    }



    fun freshData(data: ArrayList<Point>) {
        if (data.isEmpty()) {
            return
        }
        post {
            for (index in data.indices) {
                val point = data[index]

                if (index == 0) {
                    curvePath.moveTo(computeX(point.x), computeY(point.y))
                    fillPath.moveTo(computeX(point.x),(viewHeight-textHeight).toFloat())
                    fillPath.lineTo(computeX(point.x), computeY(point.y))
                } else {
                    curvePath.lineTo(computeX(point.x), computeY(point.y))
                    fillPath.lineTo(computeX(point.x), computeY(point.y))
                }

                if (index==data.size-1){
                    fillPath.lineTo(computeX(point.x),(viewHeight-textHeight).toFloat())
                }
            }
            fillPath.close()
            invalidate()
        }
    }

    private fun computeX(x:Float):Float{
        val dx = viewWidth / (endX - startX)//1点多宽
        return (x-startX)*dx
    }

    private fun computeY(y:Float):Float{
        val dx = (viewHeight-textHeight) / (endY - startY)//1点多高
        return (viewHeight-textHeight) - (y-startY)*dx
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        var currentX = computeX(startX)
        while (currentX <= viewWidth) {
            canvas?.drawLine(currentX.toFloat(), 0f, currentX + 1.toFloat(), viewHeight.toFloat() - textHeight, linePaint)
            currentX += itemWidth
        }

        canvas?.drawPath(fillPath, fillPaint)
        canvas?.drawPath(curvePath, pathPaint)

        //画文字

    }
}